package com.appstore.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.appstore.dao.ICommentDao;
import com.appstore.dao.impl.CommentDaoImpl;
import com.appstore.exception.DaoException;
import com.appstore.model.Comment;

public class CommentAction {

	private ICommentDao commentDao;
	private int id;
	private Comment comment;
	private List<Comment> commentList;
	private boolean hasSuccess;

	public CommentAction() {
		commentDao = CommentDaoImpl.getCommentDaoImpl();
	}

	public String addAsAjax() throws Exception {
		if (comment == null) {
			this.hasSuccess= false; 
		} else {
			comment.setCreatedTime(new Timestamp(new Date().getTime()));
			try {
				commentDao.save(comment);
			} catch (DaoException e) {
				throw new Exception("add comment error", e);
			}
			this.hasSuccess = true;
		}
		return "add";
	}

	public String findByAppId() throws Exception {
		try {
			this.commentList = commentDao.findByApp(id);
		} catch (DaoException e) {
			throw new Exception("find comments by application id error", e);
		}
		return "findByAppId_success";
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}

	public List<Comment> getCommentList() {
		return commentList;
	}

	public void setCommentList(List<Comment> commentList) {
		this.commentList = commentList;
	}

	public boolean isHasSuccess() {
		return hasSuccess;
	}

	public void setHasSuccess(boolean hasSuccess) {
		this.hasSuccess = hasSuccess;
	}

}
